package com.dimples.dd.system.controller;

import com.dimples.dd.common.constant.DDServerConstant;
import com.dimples.dd.common.model.Option;
import com.dimples.dd.common.pojo.PageResult;
import com.dimples.dd.common.result.CommonResult;
import com.dimples.dd.log.core.annotation.ApiLog;
import com.dimples.dd.log.core.enums.OperateTypeEnum;
import com.dimples.dd.system.model.form.OrgForm;
import com.dimples.dd.system.model.query.OrgPageQuery;
import com.dimples.dd.system.model.vo.OrgTreeVO;
import com.dimples.dd.system.service.SystemOrgService;
import com.dimples.dd.web.core.annotation.PreventDuplicateResubmit;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门控制器
 *
 * @author zhongyj <1126834403@qq.com><br/>
 * @date 2024/7/16
 */
@Tag(name = "04.机构接口")
@RestController
@Validated
@RequestMapping("org")
@RequiredArgsConstructor
public class SysOrgController {

    private final SystemOrgService systemOrgService;

    @Operation(summary = "获取机构下拉选项")
    @GetMapping("/options")
    @PreAuthorize("@ss.hasPerm('system:org:query')")
    @ApiLog(operateType = OperateTypeEnum.SELECT, applicationName = DDServerConstant.DD_SERVER_SYSTEM)
    public CommonResult<List<Option<String>>> listDeptOptions() {
        List<Option<String>> list = this.systemOrgService.listOrgOptions();
        return CommonResult.success(list);
    }

    @Operation(summary = "获取机构列表")
    @PreAuthorize("@ss.hasPerm('system:org:query')")
    @GetMapping("list")
    @ApiLog(operateType = OperateTypeEnum.SELECT, applicationName = DDServerConstant.DD_SERVER_SYSTEM)
    public PageResult<OrgTreeVO> listDepartments(
            @ParameterObject OrgPageQuery queryParams
    ) {
        List<OrgTreeVO> list = this.systemOrgService.listOrgs(queryParams);
        return PageResult.success(list);
    }

    @Operation(summary = "获取机构表单数据")
    @GetMapping("/{orgId}/form")
    @PreAuthorize("@ss.hasPerm('system:org:query')")
    @ApiLog(operateType = OperateTypeEnum.SELECT, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<OrgForm> selectOrgForm(
            @Parameter(description = "机构ID") @PathVariable String orgId
    ) {
        OrgForm orgForm = this.systemOrgService.selectOrgForm(orgId);
        return CommonResult.success(orgForm);
    }

    @Operation(summary = "新增机构")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('system:org:create')")
    @ApiLog(operateType = OperateTypeEnum.CREATE, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    @PreventDuplicateResubmit
    public CommonResult<String> createOrg(
            @Valid @RequestBody OrgForm formData
    ) {
        String id = this.systemOrgService.createOrg(formData);
        return CommonResult.success(id);
    }

    @Operation(summary = "修改机构")
    @PutMapping(value = "/{orgId}")
    @PreAuthorize("@ss.hasPerm('system:org:update')")
    @ApiLog(operateType = OperateTypeEnum.UPDATE, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<String> updateOrg(
            @PathVariable String orgId,
            @Valid @RequestBody OrgForm formData
    ) {
        orgId = this.systemOrgService.updateOrg(orgId, formData);
        return CommonResult.success(orgId);
    }

    @Operation(summary = "删除机构")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('system:org:delete')")
    @ApiLog(operateType = OperateTypeEnum.DELETE, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<Boolean> deleteOrgs(
            @Parameter(description = "机构ID，多个以英文逗号(,)分割") @PathVariable("ids") String ids
    ) {
        boolean result = this.systemOrgService.deleteByIds(ids);
        return CommonResult.success(result);
    }

}
